package com.sp.scmdb.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.Menu;
import com.sp.scmdb.entity.Parameter;
import com.sp.scmdb.entity.Role;
import com.sp.scmdb.entity.User;
import com.sp.scmdb.service.ParameterService;
import com.sp.scmdb.service.UserService;

@Controller("loginAction")
@Scope("prototype")
public class LoginAction extends BaseAction<UserBean> {

	private static final long serialVersionUID = 1L;

	private UserService userService;
	private ParameterService parameterService;

	/**
	 * @param parameterService
	 *            the parameterService to set
	 */
	@Autowired
	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;

	}

	@SuppressWarnings("unchecked")
	public String doLogin() {
		User user = userService.login(getEntity());
		if (user != null) {
			UserBean userBean = new UserBean();
			userBean.setUserName(user.getUserName());
			userBean.setId(user.getId().toString());
			Role role = new Role();
			role.setDepartment(user.getRole().getDepartment());
			role.setId(user.getRole().getId());
			role.setLevel(user.getRole().getLevel());
			role.setMenu(user.getRole().getMenu());
			role.setName(user.getRole().getName());
			role.setOperation(user.getRole().getOperation());
			userBean.setRole(role);
			List<Menu> menus = userService.getMenuByRole(role);

			// 初始化标题
			String Hql = "from Parameter where asUse = 1";
			List<Parameter> parList = parameterService.queryParameterName(Hql);
			if (parList.size() > 0) {
				userBean.setHeadName(parList.get(0).getHeadName());
			} else {
				userBean.setHeadName("东滩湿地公园定位系统");
			}
			session.put("menus", menus);
			session.put("user", userBean);
			session.put(CommonParamter.USER_ENTITY, user);
			return SUCCESS;
		} else {
			session.put("CHECK_ERRORFLAG", "2");
			session.put("TITLE", "登录信息");
			session.put("URL", "");
			session.put("MSG", "用户名/密码错误！");
			return NONE;
		}
	}

	public String doLogout() {
		// 清除当前登陆用户信息
		session.remove("user");
		session.remove("role");
		session.remove("mapSysMenu");
		session.remove("departments");

		return "logout";
	}

	public String clearSession() {
		session.remove("CommonParamter.CHECK_ERRORFLAG");
		session.remove("CommonParamter.TITLE");
		session.remove("CommonParamter.URL");
		session.remove("CommonParamter.MSG");

		return null;
	}

	@SuppressWarnings("unchecked")
	public String initLogin() {
		Parameter parameter = parameterService.queryParameterIsUse();
		session.put("parameter", parameter);
		return "logout";
	}
}
